﻿using BLL;
using DAL;
using Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1.WebUI
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        //当前页码
        public int PageIndex
        {
            get
            {
                if (ViewState["PageIndex"] == null)
                {
                    return 1;
                }
                else
                {
                    if (PageNum > 0)
                    {
                        int tpi = int.Parse(ViewState["PageIndex"].ToString());
                        if (tpi < 1)
                        {
                            return 1;
                        }
                        if (tpi > PageNum)
                        {
                            return PageNum;
                        }
                        return tpi;
                    }
                    return 1;
                }
            }
            set
            {
                ViewState["PageIndex"] = value;
            }
        }

        //每页条数
        public int PageSize
        {
            get
            {
                if (ViewState["PageSize"] == null)
                {
                    return 10;
                }
                else
                {
                    return int.Parse(ViewState["PageSize"].ToString());
                }
            }
            set
            {
                ViewState["PageSize"] = value;
            }
        }

        //总共条数
        public int PageNum
        {
            get
            {
                if (ViewState["PageNum"] == null)
                {
                    return 0;
                }
                else
                {
                    return int.Parse(ViewState["PageNum"].ToString());
                }
            }
            set
            {
                ViewState["PageNum"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //首次加载网页
                PageNum = GetUserModelNumber();
                PageIndex = 1;
                PageSize = 2;
                Label1.Text = $"总共 {PageNum} 条数据，当前是第 {PageIndex} 页";
                Repeater1.DataSource = UserManage.GetUserListWithPage(PageIndex, PageSize);
                Repeater1.DataBind();
            }
        }

        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            string comName = e.CommandName.ToString();//命令名称
            string comArg = e.CommandArgument.ToString();//命令参数
            if (comName == "del")
            {
                //删除命令
                if (UserManage.XiaoKa(comArg))
                {
                    //删除成功
                    Repeater1.DataSource = UserManage.GetUserList();
                    Repeater1.DataBind();
                }
                else
                {
                    //删除失败
                    Response.Write("<script>alert('删除失败')</script>");
                }
            }
            else if (comName == "edit")
            {
                //编辑命令
                Session["clientid"] = comArg;
                Response.Redirect("WebForm3.aspx");
            }
            //Response.Write($"<script>alert('命令名称:{comName} 命令参数:{comArg}')</script>");
        }

        protected void Button1_Click1(object sender, EventArgs e)
        {
            string st = TextBox1.Text;
            Repeater1.DataSource = UserManage.GetUserListBySearch(st);
            Repeater1.DataBind();
        }

        public int GetUserModelNumber()
        {
            DataTable dt = DBHelper.GetDataTable("select * from user_view");
            return dt.Rows.Count;
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            PageIndex = 1;
            PageSize = 2;
            Label1.Text = $"总共 {PageNum} 条数据，当前是第 {PageIndex} 页";
            Repeater1.DataSource = UserManage.GetUserListWithPage(PageIndex, PageSize);
            Repeater1.DataBind();
        }

        protected void LinkButton2_Click(object sender, EventArgs e)
        {
            PageIndex -= 1;
            if (PageIndex < 1)
            {
                PageIndex += 1;
                return;
            }

            PageSize = 2;
            Label1.Text = $"总共 {PageNum} 条数据，当前是第 {PageIndex} 页";
            Repeater1.DataSource = UserManage.GetUserListWithPage(PageIndex, PageSize);
            Repeater1.DataBind();
        }

        protected void LinkButton3_Click(object sender, EventArgs e)
        {
            PageIndex += 1;
            if (PageIndex > Convert.ToInt32(Math.Ceiling(PageNum * 1.0 / PageSize)))
            {
                PageIndex -= 1;
                return;
            }

            PageSize = 2;
            Label1.Text = $"总共 {PageNum} 条数据，当前是第 {PageIndex} 页";
            Repeater1.DataSource = UserManage.GetUserListWithPage(PageIndex, PageSize);
            Repeater1.DataBind();
        }

        protected void LinkButton4_Click(object sender, EventArgs e)
        {
            PageIndex = Convert.ToInt32(Math.Ceiling(PageNum * 1.0 / PageSize));
            PageSize = 2;
            Label1.Text = $"总共 {PageNum} 条数据，当前是第 {PageIndex} 页";
            Repeater1.DataSource = UserManage.GetUserListWithPage(PageIndex, PageSize);
            Repeater1.DataBind();
        }
    }
}